package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSONObject;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.service.ConfigureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Map;

/**
 * 系统设置对应控制类
 * Created by lilongfei on 2018/7/20.
 */
@Controller
@RequestMapping(value = "configure")
public class ConfigureController extends CommController {

    @Autowired
    ConfigureService configureService;

    /**
     * 读取配置信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "readProperties")
    public PayloadBean readProperties(@RequestBody(required = false) JSONObject obj) {
        try {
            return buildResponse(configureService.readProperties());
        } catch (IOException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 写入服务器配置
     *
     * @param body
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "writeProperties")
    public PayloadBean writeProperties(@RequestBody(required = false) JSONObject body) {
        try {
            configureService.writeProperties((Map) body);
            return buildResponse(null);
        } catch (IOException | IllegalArgumentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }
}
